/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

function getXhr()
{
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if (window.ActiveXObject)
	{
		try
		{
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	} 
	else
	{
		alert ("Votre navigateur ne supporte pas la technologie Ajax, hors notre site utilise cette dernière pour fonctionner. Nous vous invitons donc à télécharger un navigateur adéquat, comme Mozilla Firefox.");		
		return false;
	}
}

function disp(id) {
    var styl = document.getElementById(id).style;
    styl.display=(styl.display=="none")?"block":"none";
}

function display_none(id) {
    var styl = document.getElementById(id).style;
    styl.display="none";
}
function display_block(id) {
    var styl = document.getElementById(id).style;
    styl.display="block";
}

function upload_result(message, div_id) {
    document.getElementById(div_id).innerHTML = message;
}
function zap() {
location.href='/panel/'+document.getElementById('mesproduits').options[document.getElementById('mesproduits').selectedIndex].value;
}
function check_mail(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1)
	   return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false;
	if (str.indexOf(at,(lat+1))!=-1)
		return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;
	if (str.indexOf(dot,(lat+2))==-1)
		return false;
	if (str.indexOf(" ")!=-1)
		return false;
	return true;		
}

function pass_oublie()
{
	email = document.forms["fpsswd_form"].elements["password_oublie_email"];
	nic = document.forms["fpsswd_form"].elements["password_oublie_nic"];
	if (email.value == "")
	{
		alert("Vous n'avez pas rentré votre e-mail");
	}
	else if (!check_mail(email.value))
	{
		alert("Cette adresse email est invalide");
	}
	else if(nic.value == "")
	{
		alert("Vous n'avez pas rentré votre Nic-Handle");
	}
	else
	{
		document.getElementById("pass_oubli").innerHTML = "<img src='/img/loading.gif' alt='Requête en cours' /> Requête en cours";
		var xhr2 = getXhr();
		xhr2.onreadystatechange = function()
        {
            if (xhr2.readyState == 4 && xhr2.status == 200)
			{
				if (xhr2.responseText == 1)
				{
					document.getElementById("pass_oubli").innerHTML = "Un nouveau mot de passe vient de vous <br />être envoyé par email.";
				}
				else if (xhr2.responseText == 2)
				{
					document.getElementById("pass_oubli").innerHTML = "Le nic Handle n'a pas un format correct.";
				}
				else if (xhr2.responseText == 3)
				{
					document.getElementById("pass_oubli").innerHTML = "Cette adresse e-mail ou ce nic-handle ne correspond à aucun utilisateur.";
				}
				else
				{
					document.getElementById("pass_oubli").innerHTML = xhr2.responseText;
				}
			}
        }
		xhr2.open("POST", "/request-site-pass-oublie.html", true);
		xhr2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xhr2.send("ajax=1&mail="+email.value+"&nic="+nic.value);
	}
}

function zone_modif(entree_id)
{
    var xhr2 = getXhr();
    xhr2.onreadystatechange = function()
    {
        if (xhr2.readyState == 4 && xhr2.status == 200){
            document.getElementById('entree_modif').innerHTML = xhr2.responseText;
		}
        else {
            document.getElementById('entree_modif').innerHTML = '<img src="/img/loading.gif"> Chargement';
		}
    }
    xhr2.open("POST", "/request-panel-domaine-zonedns-mod.html", true);
    xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr2.send("ajax=1&id="+entree_id);
}

function renew(type_produit,id_produit)
{
    var xhr2 = getXhr();
    xhr2.onreadystatechange = function()
    {
        if (xhr2.readyState == 4 && xhr2.status == 200){
            document.getElementById('div_renew').innerHTML = xhr2.responseText;
		}
        else {
            document.getElementById('div_renew').innerHTML = '<img src="/img/loading.gif"> Chargement';
		}
    }
    xhr2.open("POST", "/request-panel-general-renew.html", true);
    xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr2.send("ajax=1&id_produit="+id_produit+"&type_produit="+type_produit);
}

function change_prod(id_produit)
{
    var xhr2 = getXhr();
    xhr2.onreadystatechange = function()
    {
        if (xhr2.readyState == 4 && xhr2.status == 200){
            document.getElementById('div_change_offre').innerHTML = xhr2.responseText;
		}
        else {
            document.getElementById('div_change_offre').innerHTML = '<img src="/img/loading.gif"> Chargement';
		}
    }
    xhr2.open("POST", "/request-panel-general-change-offre.html", true);
    xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr2.send("ajax=1&id_produit="+id_produit);
}

function heb_dom(to,action,id_heberg) {
	if(action == "add") {
		if(to == "dom_buy") {
			var xhr3 = getXhr();
			xhr3.onreadystatechange = function()
			{
				if (xhr3.readyState == 4 && xhr3.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr3.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			}
			xhr3.open("POST", "/request-panel-heberg-doms.html", true);
			xhr3.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr3.send("ajax=1&action=add&to=dom_buy&id_heberg="+id_heberg);
		}
		else if(to == "add_bhbuy_post") {
			id_dom = document.forms["add_bhbuy_post"].elements["dom_select"].value;
			dossier = document.forms["add_bhbuy_post"].elements["dossier"].value;
			var xhr4 = getXhr();
			xhr4.onreadystatechange = function()
			{
				if (xhr4.readyState == 4 && xhr4.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr4.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			
			}
			xhr4.open("POST", "/request-panel-heberg-doms.html", true);
			xhr4.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr4.send("ajax=1&action=add&to=add_bhbuy_post&id_heberg="+id_heberg+"&id_dom="+id_dom+"&dossier="+dossier);

		}
		else if(to == "dom_externe") {
			var xhr5 = getXhr();
			xhr5.onreadystatechange = function()
			{
				if (xhr5.readyState == 4 && xhr5.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr5.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			}
			xhr5.open("POST", "/request-panel-heberg-doms.html", true);
			xhr5.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr5.send("ajax=1&action=add&to=dom_externe&id_heberg="+id_heberg);
		}
		else if(to == "dom_externe_post") {
			domaine = document.forms["add_dom_externe_post"].elements["domaine"].value;
			dossier = document.forms["add_dom_externe_post"].elements["dossier"].value;
			var xhr6 = getXhr();
			xhr6.onreadystatechange = function()
			{
				if (xhr6.readyState == 4 && xhr6.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr6.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			
			}
			xhr6.open("POST", "/request-panel-heberg-doms.html", true);
			xhr6.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr6.send("ajax=1&action=add&to=dom_externe_post&id_heberg="+id_heberg+"&domaine="+domaine+"&dossier="+dossier);
		}
		else if(to == "dom_alias_bh") {
			var xhr7 = getXhr();
			xhr7.onreadystatechange = function()
			{
				if (xhr7.readyState == 4 && xhr7.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr7.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			}
			xhr7.open("POST", "/request-panel-heberg-doms.html", true);
			xhr7.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr7.send("ajax=1&action=add&to=dom_alias_bh&id_heberg="+id_heberg);
		}
		else if(to == "add_dom_alias_bh_post") {
			alias = document.forms["add_dom_alias_bh_post"].elements["alias"].value;
			ext_alias = document.forms["add_dom_alias_bh_post"].elements["ext_alias"].value;
			dossier = document.forms["add_dom_alias_bh_post"].elements["dossier"].value;
			var xhr8 = getXhr();
			xhr8.onreadystatechange = function()
			{
				if (xhr8.readyState == 4 && xhr8.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr8.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			
			}
			xhr8.open("POST", "/request-panel-heberg-doms.html", true);
			xhr8.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr8.send("ajax=1&action=add&to=dom_alias_bh_post&id_heberg="+id_heberg+"&alias="+alias+"&dossier="+dossier+"&ext_alias="+ext_alias);
		}
		
	}
	else if(action == "gest") {
		if(to == "front") {
			var xhr9 = getXhr();
			xhr9.onreadystatechange = function()
			{
				if (xhr9.readyState == 4 && xhr9.status == 200){
					document.getElementById('dom_heberg').innerHTML = xhr9.responseText; 
				}
				else {
					document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
				}
			}
			xhr9.open("POST", "/request-panel-heberg-doms.html", true);
			xhr9.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr9.send("ajax=1&action=gest&to=front&id_heberg="+id_heberg);
		}
	}	
	else if((action == "dom_del") || (action == "dom_edit") || (action == "ss_doms") || (action == "add_ss_dom") || (action == "ss_dom_del")){
		var xhr10 = getXhr();
		xhr10.onreadystatechange = function()
		{
			if (xhr10.readyState == 4 && xhr10.status == 200){
				document.getElementById('dom_heberg').innerHTML = xhr10.responseText; 
			}
			else {
				document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
			}
		}
		xhr10.open("POST", "/request-panel-heberg-doms.html", true);
		xhr10.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr10.send("ajax=1&action="+action+"&id_dom="+to+"&id_heberg="+id_heberg);
	}	
	
	else if(action == "add_ss_dom_post"){
		var xhr11 = getXhr();
		ss_dom = document.forms["add_ss_dom_post"].elements["ss_dom"].value;
		dossier = document.forms["add_ss_dom_post"].elements["dossier"].value;
		xhr11.onreadystatechange = function()
		{
			if (xhr11.readyState == 4 && xhr11.status == 200){
				document.getElementById('dom_heberg').innerHTML = xhr11.responseText; 
			}
			else {
				document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
			}
		}
		xhr11.open("POST", "/request-panel-heberg-doms.html", true);
		xhr11.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr11.send("ajax=1&action="+action+"&id_dom="+to+"&id_heberg="+id_heberg+"&ss_dom="+ss_dom+"&dossier="+dossier);
	}	
	
	else if(action == "dom_edit_post"){
		var xhr11 = getXhr();
		dossier = document.forms["edit_dom"].elements["dossier"].value;
		xhr11.onreadystatechange = function()
		{
			if (xhr11.readyState == 4 && xhr11.status == 200){
				document.getElementById('dom_heberg').innerHTML = xhr11.responseText; 
			}
			else {
				document.getElementById('dom_heberg').innerHTML = '<img src="/img/loading.gif"> Chargement';
			}
		}
		xhr11.open("POST", "/request-panel-heberg-doms.html", true);
		xhr11.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr11.send("ajax=1&action="+action+"&id_dom="+to+"&id_heberg="+id_heberg+"&id_dom="+to+"&dossier="+dossier);
	}	
}

function sur_mes() {
    var xhr12 = getXhr();
	espacedisk = document.forms["surmesure"].elements["espacedisk"].value;
	bandepassante = document.forms["surmesure"].elements["bandepassante"].value;
	bases_mysql = document.forms["surmesure"].elements["bases_mysql"].value;
	comptes_mails = document.forms["surmesure"].elements["comptes_mails"].value;
	alias_mails = document.forms["surmesure"].elements["alias_mails"].value;
	user_ftp = document.forms["surmesure"].elements["user_ftp"].value;
	doms = document.forms["surmesure"].elements["doms"].value;
	
    xhr12.onreadystatechange = function()
    {
        if (xhr12.readyState == 4 && xhr12.status == 200){
            document.getElementById('aff_prix').innerHTML = xhr12.responseText;
		}
        else {
            document.getElementById('aff_prix').innerHTML = '<img src="/img/loading.gif"> Chargement';
		}
    }
    xhr12.open("POST", "/request-offres-surmes.html", true);
    xhr12.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr12.send("ajax=1&espacedisk="+espacedisk+"&bandepassante="+bandepassante+"&bases_mysql="+bases_mysql+"&comptes_mails="+comptes_mails+"&alias_mails="+alias_mails+"&user_ftp="+user_ftp+"&doms="+doms);
}